/**
 * 
 */
$(function(){
	$('#logo-uploadImg').change(function(){
    		console.log(this)
    		var error = false;
    		var name = $("#logo-uploadImg").val();
    		if(name == null || name == "")
	  			return false;
	  		var file = document.getElementById('logo-uploadImg').files[0];
	  		if (file) {
		  		var fileType = name.substring(name.lastIndexOf(".")+1).toLowerCase();		  		
		  		if(fileType != "jpg" && fileType != "jpeg" && fileType != "png"){
		  			layer.alert("图片上传格式只支持jpg/jpeg/png",{icon:0});
	  		  		error = true;
		  		}else if (file.size > 1024 * 1024 * 5){
		  			layer.alert("文件不能大于5M",{icon:0});
      		  		error = true;
      	  		}
	  		}
	  		if(error){
	  			$('#logo-uploadImg').val("");
  		  		$('#logo-uploadImg-show').attr("src","");
  		  		$('#logo-uploadImg-show').css({'height':'0px','width':'0px'});
  		  		return false;
	  		}
	  		// 上传设置
	    	var options = {
	            // 规定把请求发送到那个URL
	            url: "/BeautyClub/product/uploadPic.do?name=titlePic",
	            // 请求方式
	            type: "post",
	            // 服务器响应的数据类型
	            dataType: "json",
	            // 请求成功时执行的回调函数
	            success: function(data, status, xhr) {	                    
	                if(data.status == true){	                	
	                	var img = $('#logo-uploadImg-show');
	            		if(file){
	            			img[0].src = window.URL.createObjectURL(file);
	            			$(img[0]).css({'height':'100px','width':'100px'});
	            		}
	                }else{
	                	$('#logo-uploadImg').val("");
	                	layer.alert(data.msg,{icon:2});
            		}
	            },
	            error: function(){
	            	$('#logo-uploadImg').val("");
	            	layer.alert("上传失败！",{icon:2});
	            }
	    	};	  		
	    	$("#titlePicForm").ajaxSubmit(options);
    	});
    	
    	$('#dec-uploadImg').change(function(){
    		console.log(this)
    		error = false;
    		var name = $("#dec-uploadImg").val();
    		if(name == null || name == "")
	  			return false;
	  		var file = document.getElementById('dec-uploadImg').files[0];
	  		if (file) {
		  		var fileType = name.substring(name.lastIndexOf(".")+1).toLowerCase();		  		
		  		if(fileType != "jpg" && fileType != "jpeg" && fileType != "png"){
		  			layer.alert("图片上传格式只支持jpg/jpeg/png",{icon:0});
	  		  		error = true;
		  		}else if (file.size > 1024 * 1024 * 5){
		  			layer.alert("文件不能大于5M",{icon:0});
      		  		error = true;
      	  		}
	  		}
	  		if(error){
	  			$("#dec-uploadImg").val("");
  		  		$('#dec-uploadImg-show').attr("src","");
  		  		$('#dec-uploadImg-show').css({'width':'0px'});
  		  		$("#dec-scroll").css({'height':'0px','width':'0px'});
  		  		return false;
	  		}
	  		// 上传设置
	    	var options = {
	            // 规定把请求发送到那个URL
	            url: "/BeautyClub/product/uploadPic.do?name=decPic",
	            // 请求方式
	            type: "post",
	            // 服务器响应的数据类型
	            dataType: "json",
	            // 请求成功时执行的回调函数
	            success: function(data, status, xhr) {	                    
	                if(data.status == true){	                	
	                	var img = $('#dec-uploadImg-show');
	            		if(file){
	            			img[0].src = window.URL.createObjectURL(file);
	            			$("#dec-scroll").css({'height':'400px','width':'400px'});
	            			$(img[0]).css({'width':'390px'})
	            		}	            		
	                }else{
	                	$("#dec-uploadImg").val("");
	                	layer.alert(data.msg,{icon:2});
            		}
	            },
	            error: function(){
	            	$("#dec-uploadImg").val("");
	            	layer.alert("上传失败！",{icon:2});
	            }
	    	};
	    	$("#decPicForm").ajaxSubmit(options);    		
    	});
});

function submitProduct() {
	var pId=$(".edit-pId").val();
	if(pId==null || pId==""){		
		layer.alert("编辑失败！请刷新页面",{icon:0});
		return false;
	}
	
	var pName = $(".product-name").val();
	if(pName==null || pName==""){
		layer.alert("请输入产品名！",{icon:0});
		return false;
	}
	var parent = $(".edit-first").val();
	var cId = $(".edit-second").val();	
	if(parent==null || parent=="" || parent=="0" ||
		cId==null || cId=="" || cId=="0"){
		layer.alert("请选择类别！",{icon:0});
		return false;
	}
	var price = $(".old-price").val();
	var currentPrice = $(".current-price").val();
	if(price==null || currentPrice==null || price=="" || currentPrice==""){
		layer.alert("请填写价格！",{icon:0});
		return false;
	}
	
	$.ajax({			
		type: "POST",
		url:"/BeautyClub/product/edit.do",
		data:$('#productForm').serialize(),// 你的formid	
	    error: function(data) {	
            layer.alert("与服务器连接失败！",{icon:2});
	    },
	    success: function(data) {
	    	if(data == "true"){                	
            	$(".product-name").val("");
            	$(".old-price").val("");
            	$(".current-price").val(""); 
            	$(".sales").val("");
            	$("#dec-uploadImg").val("");
  		  		$('#dec-uploadImg-show').attr("src","");
  		  		$('#dec-uploadImg-show').css({'width':'0px'});
  		  		$("#dec-scroll").css({'height':'0px','width':'0px'});
  		  		$('#logo-uploadImg').val("");
		  		$('#logo-uploadImg-show').attr("src","");
		  		$('#logo-uploadImg-show').css({'height':'0px','width':'0px'});
		  		layer.alert("保存成功！",{icon:1});
		  		$('.modal-container').hide();
            }else{
            	layer.alert(data,{icon:2});
            }
	    }
	});		
}